package com.xujl.mvcbind.util;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.xujl.fastlib.base.BaseRvAdapter;
import com.xujl.mvcbind.R;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class RefreshLayoutHelper<T> implements View.OnClickListener {
    public static final int MODE_REFRESH = 0;
    public static final int MODE_LOAD = 1;
    private int page = 0;
    private int pageSize = 20;
    private int mode = MODE_REFRESH;
    private List<T> mList;
    private SmartRefreshLayout mRefreshLayout;
    /**
     * 是否启用加载更多功能
     */
    private boolean useLoadMore = true;
    /**
     * 是否添加没有更多数据footer
     */
    private boolean useFooter = true;
    private int emptyLayout;
    private View emptyView;
    private BaseQuickAdapter mAdapter;
    private RecyclerView mRecyclerView;


    public RefreshLayoutHelper (List<T> list, BaseQuickAdapter adapter) {
        mList = list;
        mAdapter = adapter;
        emptyLayout = R.layout.loading_empty;
    }

    public RefreshLayoutHelper setList (List<T> list) {
        mList = list;
        return this;
    }

    public RefreshLayoutHelper setRefreshLayout (SmartRefreshLayout refreshLayout) {
        mRefreshLayout = refreshLayout;
        if (emptyLayout != 0) {
            if (mAdapter.getEmptyLayout() == null || mAdapter.getEmptyLayout().getChildCount() == 0) {
                emptyView = LayoutInflater.from(mRefreshLayout.getContext()).inflate(emptyLayout, null);
                emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                emptyView.setVisibility(View.GONE);
                mAdapter.setEmptyView(emptyView);
                final View reTryBtn = emptyView.findViewById(R.id.btn_try);
                if (reTryBtn != null) {
                    reTryBtn.setOnClickListener(this);
                }
            }
        }
        return this;
    }

    public void setPageSize (int pageSize) {
        this.pageSize = pageSize;
    }

    public RefreshLayoutHelper setUseLoadMore (boolean useLoadMore) {
        this.useLoadMore = useLoadMore;
        return this;
    }

    public RefreshLayoutHelper setUseFooter (boolean useFooter) {
        this.useFooter = useFooter;
        return this;
    }

    public RefreshLayoutHelper setAdapter (BaseRvAdapter adapter) {
        mAdapter = adapter;
        return this;
    }

    public RefreshLayoutHelper setRecyclerView (RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        if (mRecyclerView.getAdapter() == null) {
            mRecyclerView.setAdapter(mAdapter);
        }
        return this;
    }


    public static void refreshLayoutDefaultSetting (SmartRefreshLayout refreshLayout) {
        ClassicsHeader header = new ClassicsHeader(refreshLayout.getContext());
        refreshLayout.setRefreshHeader(header);
        ClassicsFooter footer = new ClassicsFooter(refreshLayout.getContext());
        refreshLayout.setRefreshFooter(footer);
        refreshLayout.setEnableOverScrollBounce(false);//关闭越界回弹
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableAutoLoadMore(false);//禁用惯性上拉加载
        refreshLayout.setDisableContentWhenRefresh(true);//是否在刷新的时候禁止列表的操作
        refreshLayout.setDisableContentWhenLoading(true);//是否在加载的时候禁止列表的操作
    }


    public void saveResult (List<T> data) {
        if (data == null) {
            data = new ArrayList<>();
        }
        if (mode == MODE_REFRESH) {
            mList.clear();
            mRefreshLayout.finishRefresh();
        } else {
            mRefreshLayout.finishLoadMore();
        }
        page++;
        mList.addAll(data);
        if (mList.size() == 0) {
            mRefreshLayout.setEnableLoadMore(false);
            if (emptyView != null) {
                emptyView.setVisibility(View.VISIBLE);
            }
        }

        //判断是否加载了所有数据
        if (data.size() == 0 || (mList.size() != 0 && mList.size() % pageSize != 0)) {
            mRefreshLayout.setEnableLoadMore(false);
            if (useFooter) {
                if (mAdapter.getFooterLayout() == null || mAdapter.getFooterLayout().getChildCount() == 0) {
                    final View footer = LayoutInflater.from(mRefreshLayout.getContext()).inflate(R.layout.item_recycle_foot, null);
                    footer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                    mAdapter.addFooterView(footer);
                } else {
                    mAdapter.getFooterLayout().setVisibility(View.VISIBLE);
                }
            }
        }
        mAdapter.notifyDataSetChanged();
        if (mode == MODE_LOAD && data.size() != 0) {
            mRecyclerView.smoothScrollToPosition(mList.size() - data.size() - 1);
        }
    }

    public void refresh () {
        if (useLoadMore) {
            mRefreshLayout.setEnableLoadMore(true);
        }
        mode = MODE_REFRESH;
        page = 0;
        if (mAdapter.getFooterLayout() != null && mAdapter.getFooterLayout().getChildCount() != 0) {
            mAdapter.getFooterLayout().setVisibility(View.GONE);
        }
    }

    public void loadMore () {
        mode = MODE_LOAD;
    }

    public void startRefresh () {
        mRefreshLayout.autoRefresh();
    }

    public int getPage () {
        return page;
    }

    public int getPageSize () {
        return pageSize;
    }

    public int getMode () {
        return mode;
    }

    @Override
    public void onClick (View view) {
        mRefreshLayout.autoRefresh();
    }

    public void stopLoadinng () {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }
}
